<template>
  <div class="buttonAddExit">
    <el-dialog :close-on-click-modal="false" class="dialog500" title="考核权重参考值" :visible.sync="dialogFormVisibleAdd" :append-to-body="true"
               @opened="$store.commit('dialogTop')" @close="resetForm('ruleFormAdd')">
      <el-form :model="ruleFormAdd" :rules="rulesAdd" ref="ruleFormAdd" label-width="90px" class="demo-ruleForm">
        <el-form-item label="考核企业" prop="enterpriseId">
          <el-select v-model="ruleFormAdd.enterpriseId" @change="selectGovWeightList" style="width: 250px;margin-top: 3px;"
                    size="mini" clearable placeholder="全部">
            <el-option
              v-for="item in $store.state.companyInfos"
              :key="item.enterpriseId"
              :label="item.enterpriseName"
              :value="item.enterpriseId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-row style="line-height: 35px;text-align: center;padding-left: 15px;">
          <el-col :span="12" v-for="(item,index) in weightList" :key="index">
            <span style="float: left;"> <el-checkbox @change="changeWeight(index)" v-model="item.hasExamine"
                    :true-label="1" :false-label="0"></el-checkbox>
            </span>
            <span style="min-width: 60px;display: inline-block;text-align: left;">{{item.govName}}</span>
              <span style="display: inline-block;min-width: 50px;margin: 0 10px;text-align: left;">{{item.vhicleCount}}</span>
           <!-- <el-input style="display: inline-block;width: 100px;margin: 0 10px;" @input="changeWeight"
                      v-model.number="item.vhicleCount" size="mini" type="Number" placeholder="单车数量"></el-input>-->
            <span>{{item.examineWeight?toDecimal(item.examineWeight * 100):0}}%</span></el-col>
        </el-row>
      </el-form>

      <!--<div style="text-align: center;margin-top: 20px;">
          <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogFormVisibleAdd = false">取 消</el-button>
          <el-button style="margin-left: 20px;" type="primary" size="small"
                     @click="submitForm('ruleFormAdd')">确 定</el-button>
        </span>
      </div>-->
    </el-dialog>
  </div>
</template>

<script>

  export default {
    props: ["taskId"],
    data () {
      return {
        ruleFormAdd: {
          "enterpriseId": "1",
        },
        dialogFormVisibleAdd: false,
        rulesAdd: {},
        weightList: []
      }
    },
    created(){

    },
    methods: {
      changeWeight(){
        let vhicleTotalCount = 0
        this.weightList.forEach(item => {
          if (item.hasExamine) {
            vhicleTotalCount += this.toDecimal(item.vhicleCount)
          }

        })

        this.weightList.forEach(item => {
          item.vhicleTotalCount = vhicleTotalCount
          if (item.hasExamine) {
            item.examineWeight = this.toDecimal(item.vhicleCount / vhicleTotalCount)
          } else {
            item.examineWeight = 0
          }
        })
      },
      dialogOpen(){
        if (this.$store.state.companyInfos.length > 0) {
          this.ruleFormAdd.enterpriseId = this.$store.state.companyInfos[0].enterpriseId
        }
        this.dialogFormVisibleAdd = true
        this.selectGovWeightList()
      },
      selectGovWeightList(){
        this.axios.post(process.env.API_HOST + '/examine/selectGovWeightList', {taskId: this.taskId})
          .then(response => {
            let result = response.data
            if (result.code == 200) {
              this.weightList = []
              result.result.forEach(item => {
                if (item.enterpriseId == this.ruleFormAdd.enterpriseId) {
                  this.weightList = item.weightList
                }

              })

            }
          })
      },
      //提交回调
      handleDiaSureAdd(){
        let forData=[]
        this.weightList.forEach(item=>{
          forData.push({
            "id": item.id,
            "hasExamine": item.hasExamine,
            "vhicleCount": item.vhicleCount,
            "vhicleTotalCount": item.vhicleTotalCount,
          })
        })
        this.axios.post(process.env.API_HOST + '/examine/updateExamineGovWeight', forData)
          .then(response => {
            let result = response.data
            if (result.code == 200) {
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
              this.dialogFormVisibleAdd = false
            }

          })
          .catch(error => {
            console.log(error);
          });
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.handleDiaSureAdd()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixin.styl"
  .buttonAddExit
  .dialog500 .el-dialog
    width 550px !important

</style>
